<?php
class App_Module_Admin_Component_Users_Ctrl_Http_Ajax extends App_Module_Admin_Component_Users_Ctrl_Http
{
    public function check_loginPrAction ()
    {
        if (!Mozg::User()->GetId())
        {
			Mozg::Lib('messanger')->createMessage(Mozg_Lang::_('SESSION_IS_OVER. LOG_IN_AGAIN_USING_THE_FORM_ON_TOP_OF_THE_PAGE.'), MESSAGE_ERROR);
	        Mozg::Response('http')->SetStatus(Mozg_Response::StatusError);

            $data = $this->GetTpl('Web')->SmallLoginForm();
            Mozg::Response('http')->SetBody($data);
        }

        Mozg_Mapper::Get('Session')->DoNotUpdate();
    }

    public function login_processPrAction ()
    {
        if (Mozg::User()->GetId())
        {
            $data = $this->GetTpl('Web')->UserStatus();
            Mozg::Response('http')->SetBody($data);
        }
        else
        {
			Mozg::Lib('messanger')->createMessage(Mozg::User()->GetErrorMessage(), MESSAGE_ERROR);
	        Mozg::Response('http')->SetStatus(Mozg_Response::StatusError);
        }
    }

    const MAX_USERS_IN_LIST = 20;

    public function userlistPrAction()
    {
    	$name = trim(urldecode(Mozg::Request('http')->getQuery('term')));

    	$preparedData = array();
    	if (Mozg::Lib('str')->Len($name) >= 2)
    	{
	    	$paginator = new Mozg_Paginator(1, self::MAX_USERS_IN_LIST);
	    	$users = Mozg_Mapper::Get('Users')->GetList(array('userName' => $name, 'searchPartUserName' => true), $paginator);

			foreach ($users as $user)
	    	{
				$preparedData[] = $user->GetUserName();
	    	}
    	}

        Mozg::Response('http')->SetBody(Mozg::Lib('Json')->Encode($preparedData));
        Mozg::Layout('admin')->SetTemplate('SimpleJson');
    }
}